<template>
  <q-form @submit="submit" class="dialog-content" style="height: 567px">
    <div class="form-inline no-padding flex justify-between margin-l-20 margin-r-20 margin-t-20">
      <select-tooltip
        v-model="queryParams.workShopId"
        :multiple="false"
        :options="workshopslist"
        @input="onChangeWork"
        msg="所在车间"
      />
      <select-input ref="selectInput"
                    :list="searchCondition"
                    @search="inputSearch"
                    @changeVal="changeVal"
                    @clearSearchVal="clearSearchVal" />
    </div>
    <div class="page-content" style="height:460px">
        <q-table
          class="myTable scroll"
          style="max-height: 460px"
          :columns="tableColumns"
          :data="tableData"
          selection="single"
          :selected.sync="selected"
          row-key="id"
          :pagination.sync="pagination"
          color="primary"
          :loading="isTableLoading"
          :hide-bottom="tableData.length > 0"
          no-data-label="暂无数据"
        >
          <template v-slot:body-cell-status="props">
            <q-td>
              <div>
                <div class="status"
                     :style="{backgroundColor: $getEnum(Enums.DEVICE_REPAIR_STATUS, props.row.status).color || ''}" />
                <span>{{$getEnum(Enums.DEVICE_REPAIR_STATUS, props.row.status).desc || ''}}</span>
              </div>
            </q-td>
          </template>
        </q-table>
        <!--  分页-->
        <yz-pagination v-model="pagination" @onSearch="getList"/>
    </div>
    <q-separator/>
    <div class="dialog-footer text-center margin-t-10">
      <q-btn outline label="取消" color="primary" @click="hideDialog" class="q-mr-md"/>
      <q-btn unelevated label="下一步" color="primary" type="submit" :loading="loading"/>
    </div>
  </q-form>
</template>

<script>
import SelectInput from 'src/components/select-input'
import { mapActions } from 'vuex'
import _ from 'lodash'
import { getDeviceRepairList } from 'src/boot/device/repair-list'
import moment from 'moment'

export default {
  name: 'repair-order-apply',
  components: { SelectInput },
  props: {
    data: Object
  },
  data () {
    return {
      loading: false,
      shows: true,
      isTableLoading: false,
      // 表格分页配置
      pagination: {
        pageNum: 1, // 当前页码
        pageSize: 15, // 每页条数
        rowsNumber: 0 // 总条数
      },
      queryParams: {
        workShopId: '',
        workShopName: '',
        searchBy: '',
        keyword: ''
      },
      selected: [],
      workshopslist: [],
      id: null,
      // 表格列
      tableColumns: [
        { name: 'repairOrder', field: 'repairOrder', label: '维修单号', align: 'left' },
        { name: 'deviceNum', field: 'deviceNum', label: '设备编号', align: 'left' },
        { name: 'deviceName', field: 'deviceName', label: '设备名称', align: 'left' },
        { name: 'deviceTypeName', field: 'deviceTypeName', label: '设备类型', align: 'left' },
        { name: 'workShop', field: 'workShop', label: '所在车间', align: 'left' },
        { name: 'status', field: 'status', label: '维修状态', align: 'left' },
        { name: 'ownerName', field: 'ownerName', label: '申请人', align: 'left' },
        { name: 'createTime', field: 'createTime', label: '申请时间', align: 'left' }
      ],
      tableData: [],
      statusOptions: [2, 3, 5],
      searchCondition: [
        { id: 'deviceNum', name: '设备编号' },
        { id: 'deviceName', name: '设备名称' }
      ]
    }
  },
  computed: {},
  mounted () {
    this.getWorkshops()
    // this.getList()
  },
  methods: {
    ...mapActions({
      getWorkshopsList: 'maintenance/getWorkshopsList'
    }),
    hideDialog () {
      this.$emit('hideDialog')
    },
    // 切换车间
    onChangeWork (val) {
      console.log(val)
      this.selected = []
      this.getList()
    },
    getWorkshops () {
      this.getWorkshopsList().then(res => {
        this.workshopslist = res
        this.queryParams.workShopId = _.get(
          this.workshopslist[0],
          'value',
          ''
        )
        this.queryParams.workShopName = _.get(
          this.workshopslist[0],
          'label',
          ''
        )
      })
    },
    getList () {
      this.isTableLoading = true
      this.selected = []
      const params = {
        pageNum: this.pagination.pageNum,
        pageSize: this.pagination.pageSize,
        status: this.statusOptions,
        workshop: this.queryParams.workShopId
      }
      !_.isEmpty(this.queryParams.keyword) && (params['keyword'] = this.queryParams.keyword)
      !_.isEmpty(this.queryParams.searchBy) && (params['searchBy'] = this.queryParams.searchBy)
      getDeviceRepairList(params, 2)
        .then(res => {
          const ret = res.data
          this.tableData = ret.records.map(v => {
            return {
              ...v,
              createTime: moment(v.createTime).format('YYYY-MM-DD HH:mm:ss')
            }
          })
          this.pagination.rowsNumber = ret.total
        })
        .finally(() => {
          this.isTableLoading = false
        })
    },
    async submit () {
      if (_.isEmpty(this.selected)) {
        this.$message.warning('请选择单号')
        return
      }
      const params = this.selected[0]
      this.$emit('successSubmit', { ...params })
    },
    // 模糊查询
    inputSearch (item, val) {
      this.selected = []
      this.queryParams.searchBy = item.id
      this.queryParams.keyword = val
      this.getList()
    },
    // 手动清空input框请求数据.
    changeVal (val) {
      if (_.isEmpty(val)) {
        this.clearSearchVal()
      }
    },
    // 清空搜索值
    clearSearchVal () {
      this.queryParams.searchBy = null
      this.queryParams.keyword = null
      this.getList()
    }
  }
}
</script>

<style scoped lang="stylus">
</style>
